Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Namespace Argus.Cryptography
    Public Class StringEncryption

        '*********************************************************************************************************************
        '
        '             Class:  StringEncryption
        '      Initial Date:  09/28/2005
        '      Last Updated:  11/13/2007
        '       Description:  Encrypt/Decrypt Strings & QueryStrings
        '      Dependencies:  N/A
        '  Additional Notes:  N/A
        '
        '*********************************************************************************************************************

        Private Shared DES As New TripleDESCryptoServiceProvider
        Private Shared MD5 As New MD5CryptoServiceProvider

        Public Shared Function MD5Hash(ByVal value As String) As Byte()
            Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
        End Function

        ''' <summary>
        ''' Encryption function.  Provide a string and a key to encrypt it.
        ''' </summary>
        ''' <param name="stringToEncrypt"></param>
        ''' <param name="key"></param>
        ''' <returns>Encrypted string</returns>
        ''' <remarks></remarks>
        Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
            DES.Key = StringEncryption.MD5Hash(key)
            DES.Mode = CipherMode.ECB
            Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
            Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
        End Function

        ''' <summary>
        ''' Decryption function.  Provide an encrypted string and the key to unencrypt it.
        ''' </summary>
        ''' <param name="encryptedString"></param>
        ''' <param name="key"></param>
        ''' <returns>Unencrypted String</returns>
        ''' <remarks></remarks>
        Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
            Try
                DES.Key = StringEncryption.MD5Hash(key)
                DES.Mode = CipherMode.ECB
                Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
                Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
            Catch ex As Exception
                Return ""
            End Try
        End Function


        ''' <summary>
        ''' Encrypt a query string.
        ''' </summary>
        ''' <param name="stringToEncrypt"></param>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function EncryptQueryString(ByVal stringToEncrypt As String, ByVal key As String) As String
            ' The + will screw up the Base 64 string, that's why we need to swap it out/in, I realize it's a total hack.
            stringToEncrypt = Encrypt(stringToEncrypt, key)
            Return stringToEncrypt.Replace("+", "@@12bQr9Z2v")
        End Function

        ''' <summary>
        ''' Decrypt a query string.
        ''' </summary>
        ''' <param name="encryptedString"></param>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function DecryptQueryString(ByVal encryptedString As String, ByVal key As String) As String
            ' The + will screw up the Base 64 string, that's why we need to swap it out/in, I realize again it's a total hack.
            encryptedString = encryptedString.Replace("@@12bQr9Z2v", "+")
            Return Decrypt(encryptedString, key)
        End Function

    End Class

    Public Class ByteEncryption

        '*********************************************************************************************************************
        '
        '             Class:  ByteEncryption
        '      Initial Date:  03/26/2008
        '      Last Updated:  03/27/2008
        '       Description:  Encrypt/Decrypt Byte Arrays
        '      Dependencies:  N/A
        '  Additional Notes:  N/A
        '
        '*********************************************************************************************************************

        Private Shared DES As New TripleDESCryptoServiceProvider
        Private Shared MD5 As New MD5CryptoServiceProvider

        Public Shared Function MD5Hash(ByVal value As String) As Byte()
            Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
        End Function

        ''' <summary>
        ''' Encrypt a byte array.
        ''' </summary>
        ''' <param name="bytesToEncrypt"></param>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function Encrypt(ByVal bytesToEncrypt As Byte(), ByVal key As String) As Byte()
            DES.Key = ByteEncryption.MD5Hash(key)
            DES.Mode = CipherMode.ECB
            Return DES.CreateEncryptor.TransformFinalBlock(bytesToEncrypt, 0, bytesToEncrypt.Length)
        End Function

        ''' <summary>
        ''' Decrypt a byte array.
        ''' </summary>
        ''' <param name="encryptedBytes"></param>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function Decrypt(ByVal encryptedBytes As Byte(), ByVal key As String) As Byte()
            DES.Key = ByteEncryption.MD5Hash(key)
            DES.Mode = CipherMode.ECB
            Return DES.CreateDecryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length)
        End Function

    End Class

End Namespace